package pub.tbc.mybatis.assist.plugin.page;

import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;

import java.sql.Connection;

/**
 * MySql分页拦截器实现，拦截StatementHandler对象的prepare方法
 *
 * @author tbc on 2017/5/15 17:19:55.
 */
@Slf4j
@Intercepts(
        @Signature(
                type = StatementHandler.class,
                method = "prepare",
                args = {Connection.class, Integer.class}
        )
)
public class MySqlPagePlugin extends AbstractPagePlugin {

    @Override
    protected String getCountSql(String sourceSql) {
        return "select count(*) total from (" + sourceSql + ") $page_count_table";
    }

    @Override
    protected String getLimitSql(String sourceSql) {
        return sourceSql + " limit ?, ?";
    }
}
